package stat;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;


import parser.XYZParserTreeConstants;
import parser.SimpleNode;

public class StmtAnalysis {
	
	private int returnCount;
	private int assignmentCount;
	private int arrayAssignmentCount;
	private int printCount;
//	private int weight = 0;

	
	public TreeModel getTreeModel(SimpleNode root) {
		DefaultMutableTreeNode dmtn = this.createTreeNodes(root);
		this.modifyTreeNodes(dmtn);		
		DefaultTreeModel dtm = new DefaultTreeModel(dmtn);
		return dtm;
	}
	
	private DefaultMutableTreeNode createTreeNodes(SimpleNode root) {
		DefaultMutableTreeNode temp = new DefaultMutableTreeNode();
		temp.setUserObject(root);
//		System.out.println("current node: " + root.toString() + "\tvalue: " + root.jjtGetFirstToken().toString());
		int childNum = root.jjtGetNumChildren();
		for (int i = 0; i < childNum; ++i)
			temp.add(createTreeNodes((SimpleNode)(root.jjtGetChild(i))));
		return temp;
	}
	
	private void modifyTreeNodes(DefaultMutableTreeNode root) {
		SimpleNode temp = (SimpleNode)root.getUserObject();
		if (root.isLeaf())
			root.setUserObject(temp.jjtGetFirstToken().toString());
		else
			root.setUserObject(temp.toString());
		int childNum = root.getChildCount();
		for (int i = 0; i < childNum; ++i)
			modifyTreeNodes((DefaultMutableTreeNode)root.getChildAt(i));
	}
	
	public StmtAnalysis(){
		this.returnCount = 0;
		this.assignmentCount = 0;
		this.arrayAssignmentCount = 0;
		this.printCount = 0;
	}
	
	public int getReturnCount(){
		return returnCount;
	}
	
	public int getAssignmentCount(){
		return assignmentCount;
	}
	
	public int getArrayAssignmentCount(){
		return arrayAssignmentCount;
	}
	
	public int getPrintCount(){
		return printCount;
	}
	
	public int countStatements(SimpleNode root){
		int weight = 0;
		if(root.jjtGetNumChildren() != 0){
			switch(root.getId()){
			case XYZParserTreeConstants.JJTIFSTATEMENT:
				for(int i=0;i<root.jjtGetNumChildren();i++)
					weight += countStatements((SimpleNode)root.jjtGetChild(i));
				weight *= 2;
				break;
			case XYZParserTreeConstants.JJTWHILESTATEMENT:
				for(int i=0;i<root.jjtGetNumChildren();i++)
					weight += countStatements((SimpleNode)root.jjtGetChild(i));
				weight *= 4;
				break;
			case XYZParserTreeConstants.JJTSTART:
			case XYZParserTreeConstants.JJTMAINCLASS:
			case XYZParserTreeConstants.JJTCLASSDECL:
			case XYZParserTreeConstants.JJTMETHODDECL:
			case XYZParserTreeConstants.JJTBLOCK:
			case XYZParserTreeConstants.JJTSTATEMENT:
				for(int i=0;i<root.jjtGetNumChildren();i++)
					weight += countStatements((SimpleNode)root.jjtGetChild(i));
				break;
			case XYZParserTreeConstants.JJTASSIGNMENTSTATEMENT:
				weight = 1;
				assignmentCount++;
				break;
			case XYZParserTreeConstants.JJTARRAYASSIGNMENTSTATEMENT:
				weight = 1;
				arrayAssignmentCount++;
				break;
			case XYZParserTreeConstants.JJTPRINTSTATEMENT:
				weight = 1;
				printCount++;
				break;
			case XYZParserTreeConstants.JJTRETURN:
				returnCount++;
				break;
			default:
				weight = 0;
				break;
			}
		}
		return weight;
	}

}
